const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:"",
    nickName:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({title:'加载中...'})
    var that = this;
    wx.getUserInfo({
      success:function(res){
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName:res.userInfo.nickName,
        })
      }
    })

    wx.login({
      success(res){
        wx.cloud.callFunction({
          name:'login',
          success(Userres){
            console.log("openID:"+Userres.result.openid)
            wx.cloud.callFunction({
              name:"find",
              data:{
                name_collection:"input_table",
                condition:{open_id:Userres.result.openid},
              },
              success(login){
                console.log(login);
                if(login.result.data.length==0){
                  wx.redirectTo({url: '/pages/register/register?data='+[Userres.result.openid,that.data.nickName,that.data.avatarUrl]}) //未注册，跳转到注册页面
                } 
                else {
                  let college_id=login.result.data[0].college_id;
                  let input_class=login.result.data[0].input_class;
                  let input_data=login.result.data[0].input_data;
                  let input_id=login.result.data[0].input_id;
                  let permissions_level = login.result.data[0].permissions_level;
                  let school_id=login.result.data[0].school_id;
                  let school_name=login.result.data[0].school_name;
                  let avatarUrl=that.data.avatarUrl;
                  let Info={
                    "college_id":college_id,
                    "input_class":input_class,
                    "input_data":input_data,
                    "input_id":input_id,
                    "permissions_level":permissions_level,
                    "school_id":school_id,
                    "school_name":school_name,
                    "avatarUrl":avatarUrl,
                  };
                 if(login.result.data[0].permissions_level=="1"){wx.redirectTo({url: '/pages/student/student?info='+JSON.stringify({Info}) //权限为1，跳转到学生界面
                })}

                //权限不为1，跳转到教师界面
                 else {wx.redirectTo({url: '/pages/teacher/teacher?info='+JSON.stringify({Info})
                })}
                }
              },
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})